<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CustomerLogin
 *
 * @author tuankiet
 */
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class LoginController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index', 'reset_password', 'new_password');
        $this->loadModel('Customers');
    }

    public function index() {

        $this->layout = 'login';

        if ($this->request->is('post')) {

            if ($this->Auth->login()) {
                if ($this->request->data['Customers']['Remember_Me'] == 1) {
                    // remove "remember me checkbox"
                    unset($this->request->data['Customers']['Remember_Me']);
                    $passwordHasher = new SimplePasswordHasher(array('hashType' => 'md5'));
                    // hash the user's password
                    $this->request->data['Customers']['Pass'] = $passwordHasher->hash(
                            $this->request->data['Customers']['Pass']
                    );

                    // write the cookie
                    $this->Cookie->write('remember_me_cookie', $this->request->data['Customers'], true, '2 weeks');
                }

                if ($this->Session->read('lastUrl')) {
                    $this->redirect($this->Session->read('lastUrl'));
                    $this->Session->delete('lastUrl');
                    exit;
                } else
                    return $this->redirect('/home/index');
            } else {
                $this->Session->setFlash(
                        __(' ログインID／パスワードに誤りがあります。'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-error'
                        )
                );
            }
        }
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
            exit;
        }
    }

    public function logout() {
        $this->Cookie->delete('remember_me_cookie');
        return $this->redirect($this->Auth->logout());
    }

    public function reset_password() {
        $this->layout = 'login';
        if ($this->request->data) {
            $email = $this->request->data['Customers']['Email'];
            $cust = $this->Customers->getCustByEmail($email);
            if (empty($cust)) {
                $this->Session->setFlash(__('Your Email is not exist.'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                $this->redirect($this->Auth->logout());
                exit();
            }
            $token = substr($cust['Customers']['Pass'], 0, 20) . base64_encode($email);
            $to = 'tuankiet1012@gmail.com';
            $subject = 'Reset Your Password';
            $message = Router::url(array('controller' => 'login', 'action' => 'new_password', 'token' => $token), true);
            $mail = $this->Email->send($to, $subject, $message);
            $this->set('mail', $mail);
            $this->Session->setFlash('Please check your email', 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-success'
            ));
            $this->redirect($this->Auth->logout());
        }
    }

    public function new_password() {
        if (empty($this->params['named']['token'])) {
            $this->redirect($this->Auth->logout());
            exit();
        }
        $string_token = $this->params['named']['token'];
        $old_pass_cut = substr($string_token, 0, 20);
        $email = base64_decode(substr($string_token, 20));
        $cust = $this->Customers->getCustByEmail($email);
        if (empty($cust) || ($old_pass_cut != substr($cust['Customers']['Pass'], 0, 20))) {
            $this->Session->setFlash(__('Your Email is not exist.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-error'
            ));
            $this->redirect($this->Auth->logout());
            exit();
        }
        $this->layout = 'login';
        if ($this->request->data) {
            if ($this->Customers->saveData($this->request->data, $cust['Customers']['CustId'])) {
                $this->redirect($this->Auth->logout());
            }
        }
    }

    //put your code here
}
